package com.examination.controller.login;

import com.examination.bean.User;
import com.examination.controller.BaseServletController;
import com.examination.service.IUserService;
import com.examination.utils.ReflectionUtil;
import com.examination.utils.ResponseResult;
import org.json.JSONObject;
import com.examination.service.impl.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录模块
 * 调用：/LoginServlet?methodName=login
 */
@WebServlet(name = "/LoginServlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends  BaseServletController {

    //注入一个业务类
    private IUserService userService = new UserService();

   /**
    * @Author GongXl
    * @Description 
    * @Date 028 22:24
    * @Param [request, response]
    * @return void
    **/
    public void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("LoginServlet userLogin call:"+request.getContextPath());
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        //角色类型 ：规定一个账号只有一个角色
        String role_type = request.getParameter("role_type");
//        System.out.println("LoginServlet userLogin call:"+account+" "+password+" "+role_type);
        //参数对象
        Map argsMap = new HashMap();
        argsMap.put("name",account);
        argsMap.put("role_type",role_type);
        User user = userService.getUser(argsMap);
//        System.out.println("LoginServlet userLogin call:"+user.toString());
        if(null != user){
            String userName = user.getName();
            String userPassword = user.getPassword();
            if(null != password && password.length() >0 && null != userPassword && userPassword.length() > 0){
                if(password.equals(userPassword)){
                    request.getSession().setAttribute("userInfo",user);
                    //密码正确，调到相关页面
                    //role_id判断角色类型

                    if(role_type != null){
                        if(role_type.equals("admin")){
                            //管理员主页
                            request.getRequestDispatcher("/examination/admin/admin_login_suc.jsp").forward(request,response);
                            return;
                        }else if(role_type.equals("teacher")){
                            //老师主页
                            request.getRequestDispatcher("/examination/teacher/teacher_login_suc.jsp").forward(request,response);
                            return;
                        }else if(role_type.equals("student")){
                            //学生主页
                            request.getRequestDispatcher("/examination/student/stu_login_suc.jsp").forward(request,response);
                            return;

                        }
                    }
                }else{
                    request.getSession().removeAttribute("userInfo");
                    //密码不正确，请重新输入
                    request.setAttribute("errorInfo","密码不正确，请重新输入。");
                    request.getRequestDispatcher("/index.jsp").forward(request,response);
                    return;
                }
            }
        }else{
            //用户不存在，调到注册页
            request.setAttribute("errorInfo","此类用户不存在，请注册。");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
            return;
        }
        request.setAttribute("errorInfo","业务异常。");
        request.getRequestDispatcher("/common/error.jsp").forward(request,response);
        System.out.println("LoginServlet userLogin end....");
        return;
    }

    /**
     * 注销
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void logOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().removeAttribute("userInfo");
        ResponseResult responseResult = new ResponseResult();
        responseResult.setState("200");
        responseResult.setMsg("success");
        JSONObject resResultJson = new JSONObject(responseResult);
        BaseServletController.rendDate("application/json;charset=utf8", resResultJson.toString(), response);
    }

}
